from rest_framework.fields import SerializerMethodField
from rest_framework.serializers import ModelSerializer

from cart.models import Cart, Comments
from course.ser import CourseSer
from user.ser import UserSer


class CartSer(ModelSerializer):
    cours = SerializerMethodField()
    userr = SerializerMethodField()

    def get_userr(self, obj):
        return obj.user.score

    def get_cours(self, obj):
        return {
            'name': obj.course.name,
            'price': obj.course.price,
            'level': obj.course.level,
            'id': obj.course.id,
            'cover': obj.course.cover,
            'video': obj.course.video,
            'score': obj.course.score
        }

    class Meta:
        model = Cart
        fields = '__all__'


class CommentsSer(ModelSerializer):
    users = SerializerMethodField(read_only=True)
    courses = SerializerMethodField(read_only=True)

    def get_users(self, obj):
        return obj.user.username,

    def get_courses(self, obj):
        return {
            'id': obj.course.id,
            'name': obj.course.name
        }

    class Meta:
        model = Comments
        fields = '__all__'
